package com.leyou.user.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leyou.auth.dto.Payload;
import com.leyou.auth.dto.UserDetail;
import com.leyou.auth.utils.JwtUtils;
import com.leyou.dto.UserDTO;
import com.leyou.exception.LyException;
import com.leyou.user.entity.User;
import com.leyou.user.mapper.UserMapper;
import com.leyou.user.service.UserService;
import com.leyou.utils.RegexUtils;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.concurrent.TimeUnit;

/**
 * @author ：qcq
 * @date ：Created in 2020/12/16 14:05
 * @description：
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {


    @Autowired
    private AmqpTemplate amqpTemplate;
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private BCryptPasswordEncoder passwordEncoder;
    @Autowired
    private JwtUtils jwtUtils;

    @Override
    public UserDTO queryUsernameAndPassword(String username, String password) {
        User user = this.query().eq("username", username).one();
        UserDTO userDTO = new UserDTO(user);
        if (user == null) {
            throw new LyException(400, "用户名或密码错误");
        }
        if (!passwordEncoder.matches(password, user.getPassword())) {
            throw new LyException(400, "用户名或密码错误");
        }
        return userDTO;
    }

    @Override
    public void register(User user, String code) {

        String realCode = redisTemplate.opsForValue().get("ly:sms:id:" + user.getPhone());
        if (!StringUtils.equalsIgnoreCase(realCode, code)) {
            throw new LyException(400, "验证码错误");

        }
        user.setPassword(passwordEncoder.encode(user.getPassword()));
        this.save(user);
    }

    @Override
    public void sendMessages(String phone) {
        if (!RegexUtils.isPhone(phone)) {
            throw new LyException(400,"手机号格式不正确");
        }
        HashMap<String, String> map = new HashMap<>();
        map.put("phone", phone);
        String code = RandomStringUtils.randomNumeric(6);
        map.put("code", code);
        amqpTemplate.convertAndSend("ly.sms.exchange", "sms.verify.code", map);
        redisTemplate.opsForValue().set("ly:sms:id:" + phone, code,5, TimeUnit.MINUTES);

    }

    @Override
    public Boolean checkUsernameOrPhone(String data, Integer type) {
        String unPhType = null;
        switch (type) {
            case 1:
                unPhType = "username";
                break;
            case 2:
                unPhType = "phone";
                break;
            default:
                throw new LyException(400, "查询参数有误");
        }
        return  this.query().eq(unPhType, data).count() == 1;
    }

    @Override
    public UserDetail checkLogin(String token) {

        Payload payload = jwtUtils.parseJwt(token);
        UserDetail userDetail = payload.getUserDetail();
        return userDetail;
    }


}
